﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using YLServerInterface.UserControls;
using Newtonsoft.Json;
using System.IO;
using BusinessClass.StateMachine;
using BusinessClass.StateMachine.Core;
using BusinessClass.StateMachine.Core.Types;

namespace BusinessClass.Business
{
    public class SimpleMakeWorker : Worker
    {
        string workingDir;
        dynamic monbanConfig;
        string ttfPath;
        IEnumerable<string> userConfig;
        public SimpleMakeWorker(MainWindow workingYard, string workingDir, dynamic monbanConfig, IEnumerable<string> userConfig)
        {
            this.workingYard = workingYard;
            this.workingDir = workingDir;
            this.monbanConfig = monbanConfig;
            this.userConfig = userConfig;
        }
        MainWindow workingYard;
        public override string GetConcreteWorkerName()
        {
            return "简单制作";
        }

        ParallelStates<IEnumerable<string>> makePicsStateParallel;
        ISate<Unit> buildExe;
        private void init(out string workname, out ErrorCodeList disp)
        {
            workname = "初始化";
            ttfPath = Common.conbindPath(@"C:\Windows\Fonts", (string)monbanConfig.ttf);
            #region 生成状态点
            List<MakePicState> makePics = new List<MakePicState>();
            MakeFileState makeUrlTxtState = null;
            foreach (var rule in monbanConfig.rules)
            {
                if (((string)rule.type).ToLower() == "png")
                {
                    makePics.Add(new MakePicState(rule, (string)monbanConfig.hint));
                }
                else
                {
                    makeUrlTxtState = new MakeFileState(rule);
                }
            }
            makePicsStateParallel = StateFactory.CreateParallelStates(makePics.ToArray());
            #endregion

            #region 打包状态点

            #region 移动需要文件到工作目录状态
            ActionSate<IEnumerable<String>> movePrepareFiles = new ActionSate<IEnumerable<string>>(() =>
                {
                    var mobanDir = Common.conbindPath(Properties.Settings.Default.MobansDir, workingYard.cbTemplates.SelectedValue.ToString());
                    var dirTool = Properties.Settings.Default.ToolDir;
                    var prepareFiles = FileHelper.MoveDirFiles(dirTool, workingDir);//移动共有文件
                    var prepareFiles2 = FileHelper.MoveDirFiles(mobanDir, workingDir);//移动模板特有文件
                    return prepareFiles.Concat(prepareFiles2);
                });
            #endregion
            var packageState = new PackageState(workingDir, "finish");
            #region 改生成文件名状态
            var changeExeName = new ActionSate<Unit>(() =>
            {
                string xl = "";
                string xn = "";
                foreach (var line in userConfig)
                {
                    if (line.StartsWith("新郎"))
                    {
                        xl = line.Split('：')[1];
                    }
                    else if (line.StartsWith("新娘"))
                    {
                        xn = line.Split('：')[1];
                    }
                }
                Common.conbindPath(workingDir, "finish.exe")
                    .NewName(string.Format("{0}♥{1}婚礼邀请.exe", xl, xn));
                return new Unit();
            });
            #endregion
            IEnumerable<string> tempFiles = new List<string>();
            buildExe = movePrepareFiles
                .ContinueWithState(files => tempFiles = tempFiles.Concat(files), packageState)
                .ContinueWithState(changeExeName);
            #endregion

            disp = ErrorCodeList.SUCCESS;
        }

        private void cutAll(out string workname, out ErrorCodeList discription)
        {
            workname = "剪裁所有图片";
            try
            {
                foreach (ImageCutter cutter in workingYard.wPnlCutterContainer.Items)
                {
                    cutter.Cut();
                }
                discription = ErrorCodeList.SUCCESS;
            }
            catch
            {
                discription = ErrorCodeList.FAILED;
            }
        }

        private void prepareWorkDir(out string workname, out ErrorCodeList disp)
        {
            workname = "准备工作目录，删除除exe以外的文件";
            var files = Directory.GetFiles(workingYard.WorkingDir, "*.exe");
            foreach (var file in files)
            {
                File.Delete(file);
            }
            disp = ErrorCodeList.SUCCESS;
        }


        public override List<Work> GetWorkSequence()
        {
            return new List<Work>() {
                        cutAll,
                        prepareWorkDir,
                        init
                        };
        }

        public override void WhenStopWorking(string workName, ErrorCodeList discription)
        {
            return;
        }
    }
}

